﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entity;
namespace Web.admin.InnerManage
{
    public partial class AddInnerWork : System.Web.UI.Page
    {
        ContestService ContestService = new ContestService();
        CompetitionService competitionService = new CompetitionService();
        WorkService workService = new WorkService();
        GroupsService groService = new GroupsService();
        StudentService stuService = new StudentService();
        ParticipantService parService = new ParticipantService();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            
            if (!IsPostBack)
            {
                string operation = Request["OPERATION"];
                string ContestId = Request["conid"];
                string WorkId = Request["id"];

                if (String.IsNullOrEmpty(ContestId))
                    return;

                BoContests contest = ContestService.getContest(ContestId);
                List<BoContests> contests = new List<BoContests>();
                contests.Add(contest);
                ContestList.DataSource = contests;
                ContestList.DataTextField = "CON_NAME";
                ContestList.DataValueField = "CON_ID";
                ContestList.DataBind();

                GroList.DataSource = groService.getContestGroups(ContestList.SelectedValue);
                GroList.DataTextField = "GRO_NAME";
                GroList.DataValueField = "GRO_ID";
                GroList.DataBind();
                
                if (operation == null)
                    return;
                if (WorkId == null)
                    return;
                if (operation == "Modify")
                {
                    ModifyWork(WorkId,ContestId);
                }
                else if (operation == "Delete")
                {
                    DeleteWork(WorkId,ContestId);
                }
            }
        }
        protected void ContestList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        protected void GroList_SelectedIndexChanged(object sender, EventArgs e)
        {
            IList<BoStudents > stus=new List<BoStudents>();
            stus = stuService.getStudentsByGroups(GroList.SelectedValue);
            System.Text.StringBuilder student = new System.Text.StringBuilder();
            foreach(BoStudents stud in stus)
            {
                student.Append( stud.STU_NAME);
            }
            TextBoxStu.Text = student.ToString();
            
        }
        protected void ModifyWork(string workId,string ContestId)
        {
            
            BoWorks work = workService.GetWork(workId);

            BoContests con = ContestService.getContest(ContestId);

            WorkId_Box.Value = work.WOR_ID;
            TextBoxName.Text = work.WOR_TITLE;
            ContestList.SelectedValue = con.CON_ID;
            TextBoxTime.Text = work.WOR_DATE.ToString();
            IsGrop.SelectedValue = work.WOR_ISGROUP.ToString();
            TextBoxStu.Text = work.WOR_STUDENTNAME;
           GroList.SelectedValue = work.WOR_GROUPS;
            KindEditor.Text = work.WOR_COMMENT;
            
        }

       
        protected void DeleteWork(string workId,string conid)
        {
            BoWorks work = workService.GetWork(workId);
            workService.DeleteWork(work);

            Response.Redirect("InnerWorks.aspx?id=" + conid);
        }

        protected void ButtonOK_Click(object sender, EventArgs e)
        {


            if (FileUpload1==null) {
                return;
            }

            if (WorkId_Box.Value == "")
            {
                BoWorks work = new BoWorks();
                work.WOR_ID = WorkId_Box.Value;
                work.WOR_TITLE = TextBoxName.Text;
                work.WOR_CONTESTS = ContestList.SelectedValue;
                //work.WOR_PERTICIPANTS = TextBoxStu.Text;
                work.WOR_DATE = Convert.ToDateTime(TextBoxTime.Text);
                work.WOR_ISGROUP =Convert.ToInt32(IsGrop.SelectedValue);
                work.WOR_GROUPS = IsGrop.SelectedValue == "1" ? GroList.SelectedValue : "";
                string[] a = SaveImageFile();
                work.WOR_URL = a[1];
                //work.WOR_TYPE = Convert.ToInt32(a[0]);
                work.WOR_COMMENT = KindEditor.Text;
                workService.CreatWork(work);

                Response.Redirect("InnerWorks.aspx?id="+ContestList.SelectedValue);
            }
            else
            {
                BoWorks work = new BoWorks();
                work.WOR_ID = WorkId_Box.Value;
                work.WOR_TITLE = TextBoxName.Text;
                work.WOR_CONTESTS = ContestList.SelectedValue;
                //work.WOR_PERTICIPANTS = TextBoxStu.Text;
                work.WOR_DATE = Convert.ToDateTime(TextBoxTime.Text);
                work.WOR_ISGROUP = Convert.ToInt32(IsGrop.SelectedValue);
                work.WOR_GROUPS = IsGrop.SelectedValue == "1" ? GroList.SelectedValue : "";
                string[] a = SaveImageFile();
                work.WOR_URL = a[1];
               // work.WOR_TYPE = Convert.ToInt32(a[0]);
                work.WOR_COMMENT = KindEditor.Text;
                workService.UpDate(work);

                Response.Redirect("InnerWorks.aspx?id="+ContestList.SelectedValue);
            }
        }
        public String[] SaveImageFile()
        {
            Boolean fileOK = true;
            String [] a=new String[2];
            String type="";
            String path = Server.MapPath("~/UploadImages/");
            if (FileUpload1.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                String[] allowedExtensions1 = { ".rar", ".zip", ".7z", ".iso" ,".tar"};
                String[] allowedExtensions2 = { ".txt", ".doc", ".docx", ".xls",".pdf",".ppt" };


                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                        type = "2";
                    }
                }
                for (int i = 0; i < allowedExtensions1.Length; i++)
                {
                    if (fileExtension == allowedExtensions1[i])
                    {
                        fileOK = true;
                        type = "1";
                    }
                }
                for (int i = 0; i < allowedExtensions2.Length; i++)
                {
                    if (fileExtension == allowedExtensions2[i])
                    {
                        fileOK = true;
                        type = "3";
                    }
                }
            }
            if (fileOK)
            {
                try
                {
                    String pre = Guid.NewGuid().ToString();
                    FileUpload1.PostedFile.SaveAs(path + pre + "_" + FileUpload1.FileName);
                    a[0] = type;
                    a[1] = pre + "_" + FileUpload1.FileName;
                    return  a;
                }
                catch (Exception ex)
                {
                    throw new Exception("文件保存失败！：" + ex.Message);
                }
            }
            else
            {
                throw new Exception("不支持的文件格式");
            }
        }
        public void DeleteImageFile(BoWorks work)
        {
            String path = Server.MapPath("~/UploadImages/");
            String imagePath = path + work.WOR_URL;
            System.IO.File.Delete(imagePath);
        }
    }
}